<template>
    <div class="flexbox flexcolumn height_100" v-loading="loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0)">
        <div class="flex over_box">
            <el-form label-position='left' label-width="140px" size="small" :model="subForm" :hide-required-asterisk='true' ref="subForm" :rules="addRules" class="add_form">
                <DeriveModel ref="rev_ref" :subForm="subForm" @setSessionStorage="setSessionStorage"/>
            </el-form>
        </div>
        <div class="project_footer com_box flexbox flexcenter">
            <el-button type="primary" class="el-button-width" size="small" @click="submitForm('subForm')" :loading="loading" :disabled='loading'>保存</el-button>
            <el-button class="el-button-width" size="small" plain @click="$router.go(-1)">返回</el-button>
        </div>
    </div>
</template>

<script>
import DeriveModel from './DeriveModel'
export default {
    components: {
        DeriveModel
    },
    name: "",
    data() {
        return {
            loading: false,
            subForm: {
                id:null,
                name: "",
                ad_ids:[],
                rule_types:1,
                rules: [
                    {
                        rule_types:null,
                        rule_where:null,
                        rule_values:null,
                    },
                ],
                set:{
                    max_deivenumber:"",
                    deive_number:"",
                    max_number:"",
                    deive_gap:1,
                    // day_budget:"",
                    marterial_good:1,
                    deive_name:"",
                    ad_updata: 1,
                    ad_uptypes: [],
                    quote: {},
                    play_roi: {},
                    class: [],
                },
                notice:[],
            },
            addRules: {
                checkFloat: [
                    {
                        pattern:
                            /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                        required: true,
                        message: "请输入正确数值",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    created() {
        this.subForm.id = this.$route.query.id
        this.subForm.name = this.$route.query.name
        this.getDetails()
    },
    beforeDestroy() {
        
    },
    methods: {
        //获取详情
        async getDetails() {
            this.loading = true
            const { data: res } = await this.$http.post(
                "autodrop/Derive/deriveShow",
                {
                    id:this.$route.query.id
                }
            );
            if (res.code !== 200) {
                this.loading = false
                this.$message.error(res.msg);
                return;
            } else {
                this.loading = false
                let ad_ids = []
                for(let i in res.data.adlist){
                    ad_ids.push({
                        ad_id:res.data.adlist[i].ad_id,
                        name:res.data.adlist[i].ad_name,
                    })
                }
                this.subForm.ad_ids = ad_ids
                this.subForm.rule_types = res.data.rule_types
                this.subForm.rules = res.data.rulelist
                this.subForm.set = res.data.set
                this.subForm.notice = res.data.notice.map(item=>{return item.user_id})
                let idArr = []
                for(let i in this.subForm.set.class){
                    idArr.push({
                        idStr:[this.subForm.set.class[i].first_industry_id,this.subForm.set.class[i].second_industry_id,this.subForm.set.class[i].third_industry_id]
                    })
                }
                this.subForm.set.class = idArr
                if(this.subForm.set.quote==null){
                    this.subForm.set.quote = {
                        dmin: "",
                        dmax: "",
                        adjust:"",
                    }
                }
                if(this.subForm.set.play_roi==null){
                    this.subForm.set.play_roi = {
                        dmin: "",
                        dmax: "",
                        adjust:"",
                    }
                }
            }
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    let subForm = JSON.parse(JSON.stringify(this.subForm))
                    subForm.ad_ids = subForm.ad_ids.map((item)=>{return item.ad_id})
                    // subForm.set.deive_name = "衍生计划-"+subForm.set.deive_name+"-<时间>"
                    let classArr = [];
                    for (let i in subForm.set.class) {
                        classArr.push({
                            first_industry_id: subForm.set.class[i].idStr[0],
                            second_industry_id: subForm.set.class[i].idStr[1],
                            third_industry_id: subForm.set.class[i].idStr[2],
                        });
                    }
                    subForm.set.class = classArr;
                    if (subForm.set.ad_updata == 0) {
                        subForm.set.ad_uptypes = []
                        subForm.set.quote = {};
                        subForm.set.play_roi = {};
                        subForm.set.class = [];
                    } else {
                        if (!this.isSelect("1")) {
                            subForm.set.quote = {};
                        }
                        if (!this.isSelect("2")) {
                            subForm.set.play_roi = {};
                        }
                        if (!this.isSelect("3")) {
                            subForm.set.class = [];
                        }
                    }
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "autodrop/Derive/addDerive",
                        subForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "保存成功！",
                            type: "success",
                        });
                        this.loading = false;
                        this.$router.go(-1);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        isSelect(types) {
            let is_true = false;
            if (this.subForm.set.ad_uptypes.includes(types)) {
                is_true = true;
            }
            return is_true;
        },

        // 缓存
        setSessionStorage() {
            
        },
    },
    
};
</script>


<style scoped>
.project_footer {
    padding: 0 20px;
    height: 72px;
}
</style>

